package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.getAgentInfoByInviterIdBean;
import com.ymatou.iapi.godmch.parameter.registerByCodeBean;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.getAgentInfoByInviterIdCall;
import com.ymatou.iapi.godmch.service.getAgentPromotePicCall;
import com.ymatou.iapi.godmch.service.registerByCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
import com.ymttest.utils.EnvSetup;
/**
*邀请人id获取邀请代理商信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentInfoByInviterId{
	private static getAgentInfoByInviterIdBean bean;
	private static getAgentInfoByInviterIdCall call;
	Integer agent_promote_mchId=Integer.parseInt(EnvSetup.getData("agent_promote_mchId"));
	mch_relationWapper relationWapper=new mch_relationWapper();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("邀请人id获取邀请代理商信息");
	}

	@Before
	public void caseUp() {
		bean=new getAgentInfoByInviterIdBean();
		call=new getAgentInfoByInviterIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentInfoByInviterId_001() {
		Logger.start(true,"邀请人id获取邀请代理商信息-主商户-申请代理-代理裂变代理-返回成功");
		try {
			
			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
			
			bean.setInviterId(applyAgent.getAgentId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();		
			
			Map<String,Object> map=new HashMap<>();
			map.put("inviterId", bean.getInviterId());
			mch_relation relation=relationWapper.selectby( map).get(0);
			
			JSONArray jArray=call.getAgents();
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Logger.verifyEquals(call.getTotal(),1, "验证total");
			Logger.verifyEquals(jArray.getJSONObject(0).getInt("agentId"), relation.getMchId(), "验证InviterId");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentInfoByInviterId_002() {
		Logger.start(true,"邀请人id获取邀请代理商信息-主商户-申请代理-代理裂变代理-代理裂变代理-返回成功");
		try {

			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			//代理1 邀请代理2
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
		   
			//代理1 邀请代理3
		    editmchBean=GodmchManageCallService.IniteditmachBean(agentId2);
            GodmchManageCallService.editmachCall(editmchBean);
			getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(agentId2);
			Thread.sleep(5000);
			registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId3=registerCall.getRegMchId();
			Logger.comment("################ agentId3:"+agentId3);

			bean.setInviterId(agentId2);
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();				
			
			Map<String,Object> map=new HashMap<>();
			map.put("inviterId", bean.getInviterId());
			mch_relation relation=relationWapper.selectby( map).get(0);
			
			JSONArray jArray=call.getAgents();
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Logger.verifyEquals(call.getTotal(),1, "验证total");
			Logger.verifyEquals(jArray.getJSONObject(0).getInt("agentId"), relation.getMchId(), "验证InviterId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentInfoByInviterId_002_1() {
		Logger.start(true,"邀请人id获取邀请代理商信息-主商户-申请代理-代理裂变代理2-返回成功");
		try {
			
			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
			
			getAgentPromotePicCall getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			Thread.sleep(5000);
			registerByCodeBean registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerByCodeCall registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId2=registerCall.getRegMchId();
			Logger.comment("################ agentId2:"+agentId2);
			
			
			getAgentPromotePicCall=GodmchManageCallService.getAgentPromotePic(applyAgent.getAgentId());
			registerBean=GodmchManageCallService.initRegisterByCode(mchId, getAgentPromotePicCall.getInviteCode());
			registerCall=GodmchManageCallService.registerByCodeCall(registerBean);
			Integer agentId3=registerCall.getRegMchId();
			
			
			bean.setInviterId(applyAgent.getAgentId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();		
			
			Map<String,Object> map=new HashMap<>();
			map.put("inviterId", bean.getInviterId());
			List<mch_relation> relation2=relationWapper.selectby( map);
			
			JSONArray jArray=call.getAgents();
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Logger.verifyEquals(call.getTotal(),2, "验证total");
			Logger.verifyEquals(jArray.getJSONObject(1).getInt("agentId"), relation2.get(0).getMchId(), "验证InviterId1");
			Logger.verifyEquals(jArray.getJSONObject(0).getInt("agentId"), relation2.get(1).getMchId(), "验证InviterId2");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentInfoByInviterId_003() {
		Logger.start(true,"邀请人id获取邀请代理商信息-主商户-申请代理-返回空");
		try {
			Integer mchId=agent_promote_mchId;
			//申请 审核代理1
			applyAgentCall applyAgent =GodmchManageCallService.applyAndapproveAgent(mchId);
			editmchBean editmchBean=GodmchManageCallService.IniteditmachBean(applyAgent.getAgentId());
            GodmchManageCallService.editmachCall(editmchBean);
            
			bean.setInviterId(applyAgent.getAgentId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();	
	
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Logger.verifyEquals(call.getTotal(),0, "验证total");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentInfoByInviterId_004() {
		Logger.start(true,"邀请人id获取邀请代理商信息-inviterId为空");
		try {

			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证statusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}